package com.jeff.android.lab.jetpack.fragment;

import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.jeff.utils.ArrayUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author: dingfeng
 * @date: 2021/3/3
 * @since: 1.0.0
 **/
public class FragmentStateAdapterImpl<T extends Fragment> extends FragmentStateAdapter {
    private List<String> mTitles;
    private List<T> fragments;
    private Fragment mCurFragment;


    public FragmentStateAdapterImpl(@NonNull FragmentActivity fragmentActivity) {
        super(fragmentActivity);
        fragments = new ArrayList<>();
        mTitles = new ArrayList<>();
    }

    public FragmentStateAdapterImpl(@NonNull Fragment fragment) {
        super(fragment);
        fragments = new ArrayList<>();
        mTitles = new ArrayList<>();
    }

    public FragmentStateAdapterImpl(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
        super(fragmentManager, lifecycle);
        fragments = new ArrayList<>();
        mTitles = new ArrayList<>();
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        return fragments.get(position);
    }

    @Override
    public int getItemCount() {
        return fragments.size();
    }

    public CharSequence getPageTitle(int position) {
        return (ArrayUtil.isEmpty(mTitles) || TextUtils.isEmpty(mTitles.get(position))) ? "" : mTitles.get(position);
    }

    public void setItems(List<T> fragments, List<String> titles) {
        this.fragments = fragments;
        this.mTitles = titles;
        notifyDataSetChanged();
    }

    public void setItems(List<T> fragments, String[] titles) {
        this.fragments = fragments;
        this.mTitles.addAll(Arrays.asList(titles));
        notifyDataSetChanged();
    }

    public void setItems(T[] fragments, String[] titles) {
        Collections.addAll(this.fragments, fragments);
        Collections.addAll(this.mTitles, titles);
        notifyDataSetChanged();
    }

    public void addItem(T fragment, String title) {
        fragments.add(fragment);
        mTitles.add(title);
        notifyDataSetChanged();
    }

    public void addItem(int index, T fragment, String title) {
        fragments.add(index, fragment);
        mTitles.add(title);
        notifyDataSetChanged();
    }

    public void delItem(int position) {
        mTitles.remove(position);
        fragments.remove(position);
        notifyDataSetChanged();
    }

    public int delItem(String title) {
        int index = mTitles.indexOf(title);
        if (index != -1) {
            delItem(index);
        }
        return index;
    }

    public void swapItems(int fromPos, int toPos) {
        Collections.swap(mTitles, fromPos, toPos);
        Collections.swap(fragments, fromPos, toPos);
        notifyDataSetChanged();
    }

    public void modifyTitle(int position, String title) {
        mTitles.set(position, title);
        notifyDataSetChanged();
    }

}
